unit goalkeeperUnit;

interface

uses common;

type
  GoalKeeper = class
    function move_before(timeLeft, goalKeeperPosition, playerDirection : integer) : integer;
    function move_after(timeLeft, goalKeeperPosition, hitDirection, ballY : integer) : integer;
  end;
var
  left, gkp, aj: integer;

implementation

{ GoalKeeper }

function GoalKeeper.move_after(timeLeft, goalKeeperPosition, hitDirection, ballY: integer): integer;
begin
  if (hitDirection >= goalKeeperPosition - HANDS) and (hitDirection <= goalKeeperPosition + HANDS) then
  begin
    move_after:= HANDS;
  end else begin
    if (hitDirection > goalKeeperPosition) then
    begin
      if (2*GOALKEEPER_WIDTH*left + goalKeeperPosition + HANDS >= hitdirection) and (2*GOALKEEPER_WIDTH*left + goalKeeperPosition - HANDS <= hitdirection) then
      begin
        move_after:= STEP_RIGHT;
        if (goalKeeperPosition + HANDS >= hitdirection) and (goalKeeperPosition - HANDS <= hitdirection) then
        begin
          move_after:= HANDS;
        end;
      end else begin
        gkp:= goalKeeperPosition + GOALKEEPER_WIDTH;
        while (gkp + GOALKEEPER_LENGTH < hitdirection) or (gkp > hitdirection) do
        begin
          gkp:= gkp + JUMP_LENGTH;
          aj:=aj+1;
        end;
        if left - aj <> 0 then
        begin
          move_after:= STAY;
        end else begin
          move_after:= JUMP_RIGHT;
        end;
      end;
    end;
    if (hitDirection < goalKeeperPosition) then
    begin
      if (goalKeeperPosition - 2*GOALKEEPER_WIDTH*left + HANDS >= hitdirection) and (goalKeeperPosition - 2*GOALKEEPER_WIDTH*left - HANDS <= hitdirection) then
      begin
        move_after:= STEP_LEFT;
        if (goalKeeperPosition + HANDS >= hitdirection) and (goalKeeperPosition - HANDS <= hitdirection) then
        begin
          move_after:= HANDS;
        end;
      end else begin
        gkp:= goalKeeperPosition - GOALKEEPER_WIDTH;
        while (gkp - GOALKEEPER_LENGTH > hitdirection) or (gkp < hitdirection) do
        begin
          gkp:= gkp - JUMP_LENGTH;
          aj:=aj+1;
        end;
        if left - aj <> 0 then
        begin
          move_after:= STAY;
        end else begin
          move_after:= JUMP_LEFT;
        end;
      end;
    end;
  end;
  left:=left-1;
end;

function GoalKeeper.move_before(timeLeft, goalKeeperPosition, playerDirection: integer): integer;
begin
  left:=DISTANCE/STRENGTH + 1;
  aj:=0;
  if playerDirection > goalKeeperPosition then
  begin
    move_before:= STEP_RIGHT;
  end else if playerDirection < goalKeeperPosition then
  begin
    move_before:= STEP_LEFT;
  end else begin
    move_before:= STAY;
  end;
end;

end.
